﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace SharpWeb
{
    public enum ListType
    {
        ul, ol, menu
    }
    /// <summary>
    /// 列表标签
    /// </summary>
    public class ListTag : AbstractContainerTagComponent
    {
        public ListTag()
        {
            _Items = new ListItemTagCollection();
        }

        protected override string TagName
        {
            get
            {
                switch (this.ListType)
                {
                    case ListType.ul:
                        return "ul";
                    case ListType.ol:
                        return "ol";
                    case ListType.menu:
                        return "menu";
                    default:
                        throw new Exception("错误的列表类型！！");
                }
            }
        }
        protected override bool CloseItSelf
        {
            get { return false; }
        }
        private ListType _ListType;
        /// <summary>
        /// 获取或设置列表类型
        /// </summary>
        public ListType ListType
        {
            get { return _ListType; }
            set { _ListType = value; }
        }

        private readonly ListItemTagCollection _Items;
        /// <summary>
        /// 获取列表项
        /// </summary>
        public ListItemTagCollection Items
        {
            get { return _Items; }
        }

        protected override void OnRenderContent(StringBuilder sb, ComponentContext context)
        {
            base.OnRenderContent(sb, context);
            foreach (var item in Items)
            {
                item.RenderComponent(sb, context);
            }
        }
    }

    public class ListItemTag : ContainerTagComponent
    {
        protected override string TagName
        {
            get
            {
                return "li";
            }
        }
    }

    public class ListItemTagCollection : KeyedCollection<string, ListItemTag>
    {

        protected override string GetKeyForItem(ListItemTag item)
        {
            return item.Name;
        }
    }
}
